//
//  CustomDataManager.m
//  test_simple_project
//
//  用户自定义数据管理类实现 - 完全可混淆
//

#import "CustomDataManager.h"

// 自定义常量 - 可混淆
static NSString * const kCustomDataManagerVersion = @"1.0.0";
static NSString * const kCustomHighPriorityThreshold = @"High Priority Item";
static NSInteger const kCustomDefaultPriority = 5;

@implementation CustomDataModel

- (instancetype)initWithIdentifier:(NSString *)identifier title:(NSString *)title {
    self = [super init];
    if (self) {
        _customIdentifier = [identifier copy];
        _customTitle = [title copy];
        _customCreationDate = [NSDate date];
        _customPriority = kCustomDefaultPriority;
    }
    return self;
}

- (NSString *)customFormattedDescription {
    NSDateFormatter *customFormatter = [[NSDateFormatter alloc] init];
    customFormatter.dateStyle = NSDateFormatterShortStyle;
    
    return [NSString stringWithFormat:@"Custom Item: %@ (%@) - Priority: %ld, Created: %@",
            self.customTitle,
            self.customIdentifier,
            (long)self.customPriority,
            [customFormatter stringFromDate:self.customCreationDate]];
}

- (BOOL)isCustomHighPriority {
    return self.customPriority >= 8;
}

@end

@interface CustomDataManager ()
@property (nonatomic, strong) NSMutableArray<CustomDataModel *> *mutableCustomDataItems;
@end

@implementation CustomDataManager

+ (instancetype)sharedCustomManager {
    static CustomDataManager *sharedCustomInstance = nil;
    static dispatch_once_t customOnceToken;
    dispatch_once(&customOnceToken, ^{
        sharedCustomInstance = [[self alloc] init];
    });
    return sharedCustomInstance;
}

+ (NSString *)customVersionString {
    return kCustomDataManagerVersion;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _mutableCustomDataItems = [[NSMutableArray alloc] init];
        [self performCustomDataValidation];
    }
    return self;
}

- (NSArray<CustomDataModel *> *)customDataItems {
    return [self.mutableCustomDataItems copy];
}

- (NSInteger)customItemCount {
    return self.mutableCustomDataItems.count;
}

- (void)addCustomDataItem:(CustomDataModel *)item {
    if (item && item.customIdentifier) {
        [self.mutableCustomDataItems addObject:item];
        [self notifyCustomDelegateWithData:self.customDataItems];
        
        NSLog(@"Added custom item: %@", item.customFormattedDescription);
    }
}

- (void)removeCustomDataItem:(CustomDataModel *)item {
    if (item) {
        [self.mutableCustomDataItems removeObject:item];
        [self notifyCustomDelegateWithData:self.customDataItems];
        
        NSLog(@"Removed custom item: %@", item.customIdentifier);
    }
}

- (void)clearAllCustomData {
    [self.mutableCustomDataItems removeAllObjects];
    [self notifyCustomDelegateWithData:self.customDataItems];
    
    NSLog(@"Cleared all custom data items");
}

- (CustomDataModel *)findCustomItemWithIdentifier:(NSString *)identifier {
    for (CustomDataModel *customItem in self.mutableCustomDataItems) {
        if ([customItem.customIdentifier isEqualToString:identifier]) {
            return customItem;
        }
    }
    return nil;
}

- (NSArray<CustomDataModel *> *)filterCustomItemsByPriority:(NSInteger)minPriority {
    NSMutableArray *filteredCustomItems = [[NSMutableArray alloc] init];
    
    for (CustomDataModel *customItem in self.mutableCustomDataItems) {
        if (customItem.customPriority >= minPriority) {
            [filteredCustomItems addObject:customItem];
        }
    }
    
    return [filteredCustomItems copy];
}

#pragma mark - Private Methods (可混淆)

- (void)performCustomDataValidation {
    // 自定义数据验证逻辑
    NSLog(@"Performing custom data validation...");
    
    // 移除无效的自定义项目
    NSMutableArray *invalidCustomItems = [[NSMutableArray alloc] init];
    for (CustomDataModel *customItem in self.mutableCustomDataItems) {
        if (!customItem.customIdentifier || customItem.customIdentifier.length == 0) {
            [invalidCustomItems addObject:customItem];
        }
    }
    
    [self.mutableCustomDataItems removeObjectsInArray:invalidCustomItems];
    
    if (invalidCustomItems.count > 0) {
        NSLog(@"Removed %lu invalid custom items", (unsigned long)invalidCustomItems.count);
    }
}

- (void)notifyCustomDelegateWithData:(NSArray *)data {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(dataManager:didUpdateData:)]) {
        [self.customDelegate dataManager:self didUpdateData:data];
    }
}

@end
